//
//  Datetime.m
//  CalendarTest
//
//  Created by mac on 13-8-27.
//  Copyright (c) 2013年 caobo. All rights reserved.
//

#import "Datetime.h"
#import "LunarCalendar.h"
#import "JBCalendar.h"
@implementation Datetime
//所有年列表
+(NSArray *)GetAllYearArray{
    NSMutableArray *monthArray = [[NSMutableArray alloc]init];
    for (int i = 1901; i<2050; i++) {
        NSString *days = [[NSString alloc]initWithFormat:@"%d",i];
        [monthArray addObject:days];
    }
    return monthArray;
}

//所有月列表
+(NSArray *)GetAllMonthArray{
    NSMutableArray *monthArray = [[NSMutableArray alloc]init];
    for (int i = 1; i<13; i++) {
        NSString *days = [[NSString alloc]initWithFormat:@"%d",i];
        [monthArray addObject:days];
    }
    return monthArray;
}





//以YYYY-MM-dd格式输出年月日
+(NSString*)getDateTime{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:[NSDate date]]];
    return date;
}



//以YYYY年MM月dd日格式输出年月日
+(NSString*)GetDateTime{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy年MM月dd日"];
     NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:[NSDate date]]];
    return date;
}

//以YYYY年MMdd格式输出此时的农历年月日
+(NSString*)GetLunarDateTime{
    JBCalendar* date = [[JBCalendar alloc]init];
    date.year = [[self GetYear] intValue],date.month =[[self GetMonth] intValue],date.day = [[self GetDay] intValue];
    LunarCalendar *lunarCalendar = [[date nsDate] chineseCalendarDate];
    NSString *lunar = [[NSString alloc]initWithFormat:
                           @"%@%@年%@%@",lunarCalendar.YearHeavenlyStem,lunarCalendar.YearEarthlyBranch,lunarCalendar.MonthLunar,lunarCalendar.DayLunar];
    return lunar;
}

//获取指定年份指定月份的星期排列表
+(NSArray *)GetDayArrayByYear:(int) year andMonth:(int) month{
    NSMutableArray *dayArray = [[NSMutableArray alloc]init];
    for (int i = 0; i< 42; i++) {
        if (i < [self GetTheWeekOfDayByYera:year andByMonth:month]-1) {
            [dayArray addObject:@" "];
        }else if ((i>[self GetTheWeekOfDayByYera:year andByMonth:month]-1)&&(i<[self GetTheWeekOfDayByYera:year andByMonth:month]+[self GetNumberOfDayByYera:year andByMonth:month])){
            NSString *days;
            if((i - [self GetTheWeekOfDayByYera:year andByMonth:month] +1)< 10)
                days = [NSString stringWithFormat:@"  %d",i-[self GetTheWeekOfDayByYera:year andByMonth:month]+1];
            else days = [NSString stringWithFormat:@"%d",i-[self GetTheWeekOfDayByYera:year andByMonth:month]+1];
            [dayArray addObject:days];
        }else {
            [dayArray addObject:@" "];
        }
    }
    return dayArray;
}


//获取指定年份指定月份的星期排列表(农历)
+(NSArray *)GetLunarDayArrayByYear:(int) year andMonth:(int) month{
    NSMutableArray *dayArray = [[NSMutableArray alloc]init];
    for (int i = 0; i< 42; i++) {
        if (i < [self GetTheWeekOfDayByYera:year andByMonth:month]-1) {
            [dayArray addObject:@" "];
        }else if ((i>[self GetTheWeekOfDayByYera:year andByMonth:month]-1)&&(i<[self GetTheWeekOfDayByYera:year andByMonth:month]+[self GetNumberOfDayByYera:year andByMonth:month])){
            NSString *days = [self GetLunarDayByYear:year andMonth:month andDay:(i-[self GetTheWeekOfDayByYera:year andByMonth:month]+1)];
            [dayArray addObject:days];
        }else {
            [dayArray addObject:@" "];
        }
    }
    return dayArray;
}

//获取某年某月某日的对应农历日
+(NSString *)GetLunarDayByYear:(int) year
                      andMonth:(int) month
                        andDay:(int) day{
    JBCalendar* date = [[JBCalendar alloc]init];
    date.year = year,date.month = month,date.day = day;
    LunarCalendar *lunarCalendar = [[date nsDate] chineseCalendarDate];
    NSString *lunarday = [[NSString alloc]initWithString:lunarCalendar.DayLunar];
    return lunarday;
}



//计算year年month月第一天是星期几，周日则为0
+(int)GetTheWeekOfDayByYera:(int)year
                 andByMonth:(int)month{
    int numWeek = ((year-1)+ (year-1)/4-(year-1)/100+(year-1)/400+1)%7;//numWeek为years年的第一天是星期几
    //DebugLog(@"%d",numWeek);
    int ar[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
    int numdays = (((year/4==0&&year/100!=0)||(year/400==0))&&(month>2))?(ar[month-1]+1):(ar[month-1]);//numdays为month月years年的第一天是这一年的第几天
    //DebugLog(@"%d",numdays);
    int dayweek = (numdays%7 + numWeek)%7;//month月第一天是星期几，周日则为0
    //DebugLog(@"%d",dayweek);
    return dayweek;
}

//判断year年month月有多少天
+(int)GetNumberOfDayByYera:(int)year
                andByMonth:(int)month{
    int nummonth = 0;
    //判断month月有多少天
    if ((month == 1)|| (month == 3)||(month == 5)||(month == 7)||(month == 8)||(month == 10)||(month == 12))
        nummonth = 31;
    else if ((month == 4)|| (month == 6)||(month == 9)||(month == 11))
        nummonth = 30;
    else if (((year/4==0&&year/100!=0)||(year/400==0))&&(month>2))
        nummonth = 29;
    else nummonth = 28;
    return nummonth;
}



+(NSString *)GetYear{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:[NSDate date]]];
    return date;
}

+(NSString *)GetYearWithDate:(NSDate*)aDate{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:aDate]];
    return date;
}


+(NSString *)GetMonth{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:[NSDate date]]];
    return date;
}

+(NSString *)GetMonthWithDate:(NSDate*)aDate{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:aDate]];
    return date;
}


+(NSString *)GetDay{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:[NSDate date]]];
    return date;
}
+(NSString *)GetDayWithDate:(NSDate*)aDate{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:aDate]];
    return date;
}


+(NSString *)GetHour{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:[NSDate date]]];
    return date;
}

+(NSString *)GetHourWithDate:(NSDate*)aDate{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:aDate]];
    return date;
}


+(NSString *)GetMinute{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"mm"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:[NSDate date]]];
    return date;
}

+(NSString *)GetSecond{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"ss"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:[NSDate date]]];
    return date;
}

+(NSDate*)GetDateWithString:(NSString*)timeString{
    //NSString* timeStr = @"2011-01-26 17:40:50";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    //设置时区,这个对于时间的处理有时很重要
    //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
    //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
    //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
    NSDate* date = [formatter dateFromString:timeString]; //------------将字符串按formatter转成nsdate
    return date;
}

+(NSDate*)GetDateWithString:(NSString*)timeString format:(NSString *)format{
    //NSString* timeStr = @"2011-01-26 17:40:50";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:format]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    //设置时区,这个对于时间的处理有时很重要
    //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
    //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
    //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
    NSDate* date = [formatter dateFromString:timeString]; //------------将字符串按formatter转成nsdate
    return date;
}

+(NSString*)GetTimeFormate:(NSDate*)timeDate{
    //时间转时间戳的方法:
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[timeDate timeIntervalSince1970]];
    return timeSp;
}
+ (NSDate*)getDateWithFormate:(NSString*)timeFormate{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timeFormate intValue]];
    return confromTimesp;
}

+ (NSString*)getStringWithFormate:(NSString*)timeFormate{
    NSDate *aDate = [Datetime getDateWithFormate:timeFormate];
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString* date = [[NSString alloc]initWithString:[formatter stringFromDate:aDate]];
    return date;
}

+ (NSDate*)getTomorrowDateWithDate:(NSDate*)aDate{
    NSDate *today = [Datetime GetDateWithString:[NSString stringWithFormat:@"%@-%@-%d 00:00:00",[Datetime GetYearWithDate:aDate],[Datetime GetMonthWithDate:aDate],[[Datetime GetDayWithDate:aDate]intValue]]];
    NSString *formate = [Datetime GetTimeFormate:today];
    
    NSDate *date = [Datetime getDateWithFormate:[NSString stringWithFormat:@"%d",[formate intValue]+24*60*60]];
    return date;
}

+ (NSDate*)getYestodayDateWithDate:(NSDate*)aDate{
    NSDate *today = [Datetime GetDateWithString:[NSString stringWithFormat:@"%@-%@-%d 00:00:00",[Datetime GetYearWithDate:aDate],[Datetime GetMonthWithDate:aDate],[[Datetime GetDayWithDate:aDate]intValue]]];
    NSString *formate = [Datetime GetTimeFormate:today];
    
    NSDate *date = [Datetime getDateWithFormate:[NSString stringWithFormat:@"%d",[formate intValue]-24*60*60]];
    return date;
}

+ (NSDate*)getDateWithDateOffset:(NSInteger )offset{
    NSDate *aDate = [NSDate date];
    NSDate *today = [Datetime GetDateWithString:[NSString stringWithFormat:@"%@-%@-%d 00:00:00",[Datetime GetYearWithDate:aDate],[Datetime GetMonthWithDate:aDate],[[Datetime GetDayWithDate:aDate]intValue]]];
    NSString *formate = [Datetime GetTimeFormate:today];
    
    NSDate *date = [Datetime getDateWithFormate:[NSString stringWithFormat:@"%ld",[formate intValue] + offset*24*60*60]];
    return date;
}



+(NSString *)dateStringWithDate:(NSDate *)date DateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
}

+(NSString *)dateStringWithtimeSp:(NSString *)timeSp DateFormat:(NSString *)dateFormat
{
    NSDate *date = [Datetime getDateWithFormate:timeSp];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
}
+ (NSString *)dateStringWithDateStr:(NSString *)dateStr DateFormat:(NSString *)dateFormat
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue] / 1000];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
    
}

/**
 * 和当前时间的比较
 * @parma selectDate:选择的日期
 * @return 1:大于当前日期 -1:小于当前时间 0:等于当前时间
 */
+ (int)compareDateWithSelectDate:(NSString *)selectDate
{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *fomatter = [[NSDateFormatter alloc] init];
    [fomatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *curentDateStr = [fomatter stringFromDate:currentDate];
    currentDate = [fomatter dateFromString:curentDateStr];
    
    int result = [[fomatter dateFromString:selectDate] compare:currentDate];
    if(result == NSOrderedDescending)
    {
        return 1;
    }
    else if(result == NSOrderedAscending)
    {
        return -1;
    }
    return 0;
}

/**
 * 传入两个date，和一个格式，进行比较  @"yyyy-MM-dd"
 * @parma
 * @return 1:大于当前日期 -1:小于当前时间 0:等于当前时间
 */
+ (int)compareDateWithcurrentDate:(NSDate*)currentDate withSelectedDate:(NSDate*)selectedDate withDateFormat:(NSString *)dateFormat{
    
    NSDateFormatter *fomatter = [[NSDateFormatter alloc] init];
    [fomatter setDateFormat:dateFormat];
    
    NSString *curentDateStr = [fomatter stringFromDate:currentDate];
    
    NSString * selectedDateStr = [fomatter stringFromDate:selectedDate];
    
    int result = [[fomatter dateFromString:selectedDateStr] compare:[fomatter dateFromString:curentDateStr]];
    
    if(result == NSOrderedDescending)
    {
        return 1;
    }
    else if(result == NSOrderedAscending)
    {
        return -1;
    }
    return 0;
    
}




+ (NSString*)getCurrentTimestamp{
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSTimeInterval a=[dat timeIntervalSince1970];
    
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    
    return timeString;
    
}


+(NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期天", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}

/**
 判断开始时间于结束时间是否负责规则
 
 @param startTime 开始时间
 @param endTime 结束时间
 @return 是否负责规则
 */
+ (BOOL)compareTimeWithStartTime:(NSString *)startTime andEndTime:(NSString *)endTime {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];
    NSDate *startData = [dateFormatter dateFromString:startTime];
    NSDate *endDate = [dateFormatter dateFromString:endTime];
    NSInteger end = [endDate timeIntervalSinceDate:startData];
    if (end>=0) {
        return YES;
    }
    return NO;
}


+ (NSString *)currentTimeString:(NSString *)format {
    NSDate *dateNow = [NSDate date];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:dateNow];
    
}

+ (NSString *)getTimeAfterHours:(NSInteger)hours format:(NSString *)format {
    NSTimeInterval secondsPerHour = hours * 60 * 60;
    NSDate * currentDate = [NSDate date];
    NSDate *dateNow = [currentDate dateByAddingTimeInterval:secondsPerHour];
    
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSString * timeStr = [dateFormatter stringFromDate:dateNow];
    
    return timeStr;
    
}


+(BOOL)isToday:(NSDate *)date{
    
    NSString *str = [Datetime dateStringWithDate:date DateFormat:@"yyyy-MM-dd"];
    NSString *today = [Datetime dateStringWithDate:[NSDate date] DateFormat:@"yyyy-MM-dd"];
    if ([str isEqualToString:today]) {
        return YES;
    }else{
        return NO;
    }
}

+ (NSString *)transToTimeSp:(NSString *)time dateFormate:(NSString *)format {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; //设置本地时区
    [dateFormatter setDateFormat:format];
    NSDate *date = [dateFormatter dateFromString:time];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];//时间戳
    return timeSp;
}

+ (NSString *)dealTimeDifference:(NSString *)dateStr {
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval createTime = [dateStr doubleValue];
    // 时间差
    NSTimeInterval time = currentTime - createTime;
    // 秒转分
    NSInteger minutes = time/60;
    if (time <= 60 ) {
        return [NSString stringWithFormat:@"刚刚"];
    }
    if (minutes>=1 && minutes < 60) {
        return [NSString stringWithFormat:@"%ld分钟前",minutes];
    }
    NSInteger hours = time/3600;
    if (hours>=1 && hours < 24) {
        return [NSString stringWithFormat:@"%ld小时前",hours];
    }
    return [Datetime dateStringWithtimeSp:dateStr DateFormat:@"yyyy/MM/dd"];
}




@end
